import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../views/login.vue'

import {
  Message
} from 'element-ui';

Vue.use(VueRouter)

const routes = [{
    path: '/',
    name: 'Home',
    component: login
  },
  {
    path: '/index',   //后台总页面
    component: () => import('../views/index.vue'),
    children: [{
        path: '/',  //首页
        component: () => import('../views/index/index.vue'),
        children: [{
            path: '/home',  //首页默认显示子路由
            component: () => import('../views/index/home.vue'),
          },
          {
            path: '/img', 
            component: () => import('../views/index/img.vue')
          },
          {
            path: '/shoplist',
            component: () => import('../views/index/shoplist.vue')
          }
        ]
      },
      {
        path:'/shop',
        component:()=>import ('../views/shop/index.vue')
      },
      {
        path:'/buy',
        component:()=>import ('../views/buy/index.vue')
      },
      ,
      {
        path:'/good',
        component:()=>import ('../views/shop/index.vue')
      },
      {
        path:'/until',
        component:()=>import ('../views/until/index.vue')
      },
    ]
  },
  

]

const router = new VueRouter({
  routes
})


router.beforeEach((to, from, next) => {
  if (to.path != '/') {
    let date=new Date()
    let nowTime=(date.getTime())/1000
    if (localStorage.getItem('token')) {
      let {time}=JSON.parse(localStorage.getItem('token'))
      // console.log(nowTime,time);
      if(nowTime-time>10){  //说明过期了
        next('/')
      }else{
        next()
      }
    } else(
      Message('你还没登录呢'),
      next('/')
    )
  } else {
    next()
  }
})
export default router